
package com.qida.clm.core.utils;

import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.qida.clm.utilslib.R;

/**
 *
 * @Description 是检测网络的一个工具包
 */

public class NetWorkUtil{
	
	public static enum netType{
		wifi, CMNET, CMWAP, noneNet
	}

	/**
	 * 网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	
	public static boolean isNetworkAvailable(Context context){
		ConnectivityManager mgr = SystemServiceUtils.getConnectivityManager(context);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null){
			for (int i = 0; i < info.length; i++){
				if (info[i].getState() == NetworkInfo.State.CONNECTED)
				{
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否有网络连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkConnected(Context context){
		if (context != null){
			ConnectivityManager connectivityManager = SystemServiceUtils.getConnectivityManager(context);
			NetworkInfo mNetworkInfo = connectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null){
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断WIFI网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiConnected(Context context){
		
		if (context != null){
			ConnectivityManager connectivityManager = SystemServiceUtils.getConnectivityManager(context);
			NetworkInfo mWiFiNetworkInfo = connectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null){
				return mWiFiNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断MOBILE网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isMobileConnected(Context context){
		if (context != null){
			ConnectivityManager connectivityManager = SystemServiceUtils.getConnectivityManager(context);
			NetworkInfo mMobileNetworkInfo = connectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null){
				return mMobileNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 获取当前网络连接的类型信息
	 * 
	 * @param context
	 * @return
	 */
	public static int getConnectedType(Context context){
		if (context != null){
			ConnectivityManager connectivityManager = SystemServiceUtils.getConnectivityManager(context);
			NetworkInfo mNetworkInfo = connectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null && mNetworkInfo.isAvailable()){
				return mNetworkInfo.getType();
			}
		}
		return -1;
	}

	/**
	 * 	
	 * 
	 *  获取当前的网络状态 -1：没有网络 1：WIFI网络2：wap 网络3：net网络
	 * 
	 * @param context
	 * 
	 * @return
	 */
	public static netType getAPNType(Context context){
		ConnectivityManager connMgr = SystemServiceUtils.getConnectivityManager(context);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null){
			return netType.noneNet;
		}
		int nType = networkInfo.getType();

		if (nType == ConnectivityManager.TYPE_MOBILE){
			if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){
				return netType.CMNET;
			}
			else{
				return netType.CMWAP;
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI){
			return netType.wifi;
		}
		return netType.noneNet;
	}

	/**
	 * 获取网络运营商
	 * @param context
	 * @return
     */
	public static String getNetworkProviders(Context context){
		ConnectivityManager connMag = SystemServiceUtils.getConnectivityManager(context);
		NetworkInfo info = connMag.getActiveNetworkInfo();
		Resources resources = context.getResources();
		if (info != null) {
			switch (info.getType()) {
				case ConnectivityManager.TYPE_MOBILE: {
					switch (info.getSubtype()) {
						case TelephonyManager.NETWORK_TYPE_GPRS://联通2G
						case TelephonyManager.NETWORK_TYPE_UMTS://联通3G
						case TelephonyManager.NETWORK_TYPE_HSDPA://联通3G
							return resources.getString(R.string.unicom);
						case TelephonyManager.NETWORK_TYPE_EDGE://移动2G
							return resources.getString(R.string.mobile);
						case TelephonyManager.NETWORK_TYPE_EVDO_A://电信3G
						case TelephonyManager.NETWORK_TYPE_EVDO_0://电信3G
						case TelephonyManager.NETWORK_TYPE_CDMA://电信2G
							return resources.getString(R.string.telecom);
						case TelephonyManager.NETWORK_TYPE_UNKNOWN://0 未知
						case TelephonyManager.NETWORK_TYPE_IDEN://LTE
						case TelephonyManager.NETWORK_TYPE_HSUPA://LTE
						case TelephonyManager.NETWORK_TYPE_HSPA://LTE
							return resources.getString(R.string.unknown);
					}
					break;
				}
				case ConnectivityManager.TYPE_WIFI: {
					return resources.getString(R.string.wifi);
				}
				case -1://没有APN的..
				default:
					break;
			}
		}
		return  resources.getString(R.string.unknown);
	}
}
